/*
 * ArtistTableModel.java
 *
 * Created on June 11, 2005, 12:34 PM
 */
package com.bff.mpd.musicjukebox.lib;

import com.bff.mpd.utils.Utils;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bff.javampd.MPDDatabase;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDDatabaseException;
import org.bff.javampd.objects.MPDArtist;
import org.bff.javampd.objects.MPDGenre;
import org.bff.javampd.objects.MPDItem;
import org.bff.javampd.objects.MPDSong;

/**
 *
 * @author Bill
 */
public class ArtistTableModel extends SongTableModel {

    public static final int ALBUM = 0;
    public static final int TITLE = 1;
    public static final int LENGTH = 2;
    public static final int YEAR = 3;
    public static final int GENRE = 4;
    private List<MPDSong> dataList;
    private MPDDatabase database;
    public final static String[] columnNames = {
        Utils.getMessage("TM_ALBUM"),
        Utils.getMessage("TM_TITLE"),
        Utils.getMessage("TM_LENGTH"),
        Utils.getMessage("TM_YEAR"),
        Utils.getMessage("TM_GENRE")
    };

    private ArtistTableModel() {
        this.database = JukeboxProperties.getInstance().getDatabase();
    }

    /** Creates a new instance of TassTableModel */
    public ArtistTableModel(MPDItem item) {
        this();
        try {
            if (item instanceof MPDArtist) {

                dataList = new ArrayList<MPDSong>(getDatabase().findArtist((MPDArtist) item));

            } else if (item instanceof MPDGenre) {
                dataList = new ArrayList<MPDSong>(getDatabase().findGenre((MPDGenre) item));
            }
        } catch (MPDConnectionException ex) {
            Logger.getLogger(ArtistTableModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDDatabaseException ex) {
            Logger.getLogger(ArtistTableModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** Creates a new instance of TassTableModel */
    public ArtistTableModel(String year) {
        this();
        try {
            dataList = new ArrayList<MPDSong>(getDatabase().findYear(year));
        } catch (MPDConnectionException ex) {
            Logger.getLogger(ArtistTableModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDDatabaseException ex) {
            Logger.getLogger(ArtistTableModel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public ArtistTableModel(java.util.Collection songs) {
        dataList = new ArrayList(songs);
    }

    @Override
    public int getRowCount() {
        return (dataList.size());
    }

    @Override
    public Object getValueAt(int row, int col) {
        MPDSong song = dataList.get(row);
        switch (col) {
            case (ALBUM):
                return (song.getAlbum());
            case (TITLE):
                return (song.getTitle());
            case (LENGTH):
                return (song.getLength());
            case (YEAR):
                return (song.getYear());
            case (GENRE):
                return (song.getGenre());
            default:
                return (null);
        }
    }

    @Override
    public int getColumnCount() {
        return (columnNames.length);
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public MPDSong getSong(int row) {
        return dataList.get(row);
    }

    @Override
    public int getArtistColumnIndex() {
        return (-1);
    }

    @Override
    public int getAlbumColumnIndex() {
        return (ALBUM);
    }

    @Override
    public int getTitleColumnIndex() {
        return (TITLE);
    }

    @Override
    public int getLengthColumnIndex() {
        return (LENGTH);
    }

    @Override
    public int getTrackColumnIndex() {
        return (-1);
    }

    @Override
    public int getYearColumnIndex() {
        return (YEAR);
    }

    @Override
    public int getGenreColumnIndex() {
        return (GENRE);
    }

    public int getBitrateColumnIndex() {
        return (-1);
    }

    /**
     * @return the database
     */
    public MPDDatabase getDatabase() {
        return database;
    }

    /**
     * @param database the database to set
     */
    public void setDatabase(MPDDatabase database) {
        this.database = database;
    }

    @Override
    public int getSortColumn() {
        return ALBUM;
    }
}
